package com.foyekoo.project001.utils;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class PlayMusicTool {

	private static MediaPlayer mMediaPlayer =null ;
	private static String pathTop= null;//歌曲所在目录
	
	public static boolean isPlay = true;
	
	private static PlayMusicTool instance = null;
	
	/**获取PlayMusicTool实例*/
	public static PlayMusicTool getInstance(){
		if(instance == null){
			instance = new PlayMusicTool();
		}
		return instance;
	}
	/**
	 * 根据音乐名称来播放音乐
	 * */
	public  void playMusicToTitle(String title,Context context) {
			close();
			if(mMediaPlayer==null){
				mMediaPlayer  = new MediaPlayer();
			}
		
			pathTop = Environment.getExternalStorageDirectory().getPath()+"/voice/";
			String path = null;
			path = pathTop+ title +".mp3";
			
			File file = new File(path);
			if(file.exists()){
				setDataSource(path);
				mMediaPlayer.start();
			}else{
				ToastUtils.show(context, "音乐路径不存在");
				return;
			}
	}
	public  void playMusicToTitle(String title,Context context,String name) {
		close();
		if(mMediaPlayer==null){
			mMediaPlayer  = new MediaPlayer();
		}
	
		pathTop = Environment.getExternalStorageDirectory().getPath()+"/"+name+"/";
		String path = null;
		path = pathTop+ title +".mp3";
		
		File file = new File(path);
		if(file.exists()){
			setDataSource(path);
			mMediaPlayer.start();
		}else{
			ToastUtils.show(context, "音乐路径不存在");
			return;
		}
}
	public void playMusicToTales(Context context, String name) {
		if (mMediaPlayer == null) {
			mMediaPlayer = new MediaPlayer();
		} else {
			if (mMediaPlayer.isPlaying())
				mMediaPlayer.stop();
		}
		pathTop = Environment.getExternalStorageDirectory().getPath() + "/voice/" + name;
		File[] files = new File(pathTop).listFiles();
		String path = null;

		if (files != null && files.length > 0) {
			int num = (int) (Math.random() * files.length);
			path = files[num].getPath();
			if (!path.endsWith(".mp3")) {
				ToastUtils.show(context, "不是音频文件");
				return;
			}
			ToastUtils.show(context, files[num].getName());
			Log.e("路径", "路径>=" + path);
			File file = new File(path);
			if (file.exists()) {
				setDataSource(path);

				mMediaPlayer.start();
			} else {
				ToastUtils.show(context, "音频路径不存在");
				return;
			}
		} else
			return;

	}
	/**
	 * 根据音乐id播放音乐
	 * */
	public  void playMusicToId(Context context,int id ){
		if(isPlay){
			close();
			mMediaPlayer  = MediaPlayer.create(context, id);
			try {
				mMediaPlayer.stop();
				mMediaPlayer.prepare();
				mMediaPlayer.start();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}

	}

	
	/**设置路径*/
	public static void setDataSource(String path)
	{

		try
		{
			mMediaPlayer.reset();
			mMediaPlayer.setDataSource(path);
			mMediaPlayer.prepare();
		}
		catch (IOException e)
		{
			return;
		}
		catch (IllegalArgumentException e)
		{
			return;
		}
	}
	
	/**
	 * 释放mMediaPlayer
	 * */
	public static void close(){
		if(mMediaPlayer!=null){
			mMediaPlayer.stop();
			mMediaPlayer.release();
			mMediaPlayer=null;
		}
		
	}
	
}
